//
//  HDBaseViewController.m
//  HDUIKit
//
//  Created by dongshachen on 2021/12/31.
//

#import "HDBaseViewController.h"
#import "HDBaseDefine.h"
#import <Masonry/Masonry.h>

@interface HDBaseViewController ()

@property(nonatomic,assign)CGFloat safetop;    //顶部安全高度
@property(nonatomic,assign)CGFloat navHeight;  //总高度
@property (nonatomic, strong) HDNavNormalView *navNormalView;

@end


@implementation HDBaseViewController{
    BOOL _isNeedNav;
    BOOL _oldNavgationBarHidenState;
}

- (void)setIsNeedNav:(BOOL)isNeedNav {
    _isNeedNav = isNeedNav;
    if (!_isNeedNav) {
        if(self.navNormalView){
            [self.navNormalView removeFromSuperview];
            _navNormalView = nil;
        }
    }
}
- (HDNavNormalView *)navNormalView
{
    if (!_isNeedNav) {
        return nil;
    }
    if (!_navNormalView) {
        _navNormalView = [HDNavNormalView new];
        [_navNormalView.leftBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    [self.view addSubview:_navNormalView];
    [_navNormalView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(self.view.mas_top);
        make.width.equalTo(self.view.mas_width);
        make.height.mas_equalTo(self.navHeight);
    }];
    return _navNormalView;
}

- (instancetype)init {
    if ([super init]) {
        self.StatusBarIsBlack=NO;
        _isNeedNav = YES;
        self.safetop = HDSafeTopHeight;
        self.navHeight=self.safetop+HDNavHeight;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _oldNavgationBarHidenState = self.navigationController.isNavigationBarHidden;
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.fd_prefersNavigationBarHidden=YES;
    self.automaticallyAdjustsScrollViewInsets=NO;
}

-(void)backBtnClick {
    if (self.navigationController&&(self.navigationController.viewControllers.count>1)) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if(self.navigationController)
    {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
    }
    else
    {
        [self dismissViewControllerAnimated:YES completion:nil];
    }
}

- (void)changeStatusBarIsBlack:(BOOL)isBlack {
    self.StatusBarIsBlack = isBlack;
    [self setNeedsStatusBarAppearanceUpdate];
}

-(UIStatusBarStyle)preferredStatusBarStyle {
    if (self.StatusBarIsBlack) {
        return UIStatusBarStyleDefault;
    }
    return UIStatusBarStyleLightContent;
}

- (BOOL)prefersStatusBarHidden {
    return self.StatusBarIsHidden;
}

-(BOOL)shouldAutorotate {
    return YES;
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [self.navigationController setNavigationBarHidden:_oldNavgationBarHidenState];
}


@end
